package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/23 15:58
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Reference
    UserService userService;

    // 获取用户信息（用户名）
    @RequestMapping(value = "/getUsername")
    public Result getUsername(){
        // 获取用户信息（从SpringSecurity）
        try {
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }

    }


    // 新增检查组
    @RequestMapping(value = "/add")
    public Result add(@RequestBody com.itheima.health.pojo.User user,  Integer [] roleIds){
        try {
            userService.add(user,roleIds);
            return new Result(true, MessageConstant.ADD_User_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }




    // 使用用户id，查询检查项的id集合，返回List<Integer>
    @RequestMapping(value = "/findCheckitemIds")
    public List<Integer> findCheckitemIds(Integer id){
        List<Integer> list = userService.findCheckitemIds(id);
        return list;
    }



    // 分页查询（根据查询条件：传递查询条件，按照条件查询；没有查询条件，查询所有）
    @RequestMapping(value = "/findPage")

    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    // 删除用户项
    @RequestMapping(value = "/delete")

    public Result delete(Integer id){
        try {
            userService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_User_SUCCESS);
        }catch(RuntimeException e){
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    // 使用ID查询检查项
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        com.itheima.health.pojo.User user = userService.findById(id);
        if(user!=null){
            return new Result(true, "",user);
        }else{
            return new Result(false, "");
        }
    }


    @RequestMapping(value = "/edit")

    public Result edit(@RequestBody com.itheima.health.pojo.User user,  Integer [] roleIds){
        try {
            userService.edit(user,roleIds);
            return new Result(true, MessageConstant.EDIT_User_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }


}
